#ifndef SEND_H
#define SEND_H

#include <QString>
#include "QtSerialPort/QSerialPort.h" // 包含 Qt 的 QSerialPort 头文件
#include "QtNetwork/QUdpSocket"
#include <QtNetwork/QHostAddress>
#include <QtNetwork/QNetworkDatagram>

#include <QMessageBox>


// 将文件编码转为执行编码(正常显示中文)
#if defined(WIN32) // windows系统
#	define TRANSFER(str) QString::fromUtf8(str)
#elif defined(linux) // linux系统
#	define TRANSFER(str) QString::fromLocal8Bit(str)
#endif

//空间向量结构体
typedef struct {
  float x;
  float y;
  float z;
}vector_3f;

enum robo_status {
   FORWARDING = 0, //直行
    BACKING = 1, //后退
    Probe = 2, //试探
    Turn_left_90 = 3,//左转90
    Turn_right_90 = 4,//右转90
    Turn_180 = 5,//180掉头
    Find = 6,//找墙
    PENDING = 7,//挂起
    RAISING=8,//升起
    CLIMBING=9,//爬墙
    LATERAL=10,//侧面
    DIVING=11,//潜水
    BYPASSING=12,//绕过
    ALIGNING=13,//对齐
    TURNING=14,//转弯
    PAUSE=15,//暂停
    INTERRUPT=16,//中断
    REST=17, //复位
    CHECK_MOTORS=18,//检查电机
    CHECK_PUMPS=19,//检水泵
    INIT=20,//初始化
};

class udp_mavlink : public QObject {
  Q_OBJECT

public:
  // UDP 套接字
  QUdpSocket *server;
private:

  QByteArray  m_frame;      // 保存最新一帧完整 MAVLink 消息的缓存
  qint64      m_frameSize = 0;
  QByteArray ABC_message;//字符形式输出消息;

public:
  udp_mavlink(const QString &udpAdd, const QString &udpPort, const QString &udpPortlocal);
  ~udp_mavlink();

public:
  QString udp_add;
  QString udp_port_remote;
  QString udp_port_local;
  QString udp_rx_data; // 接收到的 UDP 数据
  void mavlink_send_udp(char *hexDatah, char len);

signals :
    void message_share(const QByteArray *data,  uint8_t len);

private slots:
    void messageReceive();
    void mavlink_msg_analysis(uint8_t * buff_add,uint8_t len);
public:
  //机器人状态消息
  struct {
    QString fw_version;
    QString hw_version;
    QString bt_version;
    QString clean_mode;
    QString now_state;
    QString battery_volt;
    QString battery_percent;
    QString battery_current;
    QString battery_power;
    QString has_water_in;
    QString has_water_left;
    QString has_water_right;

    int16_t moto_rmp_left = 0;
    int16_t moto_rmp_right= 0;
    int16_t pump_rmp_left= 0;
    int16_t pump_rmp_right= 0;
    int16_t flow_x= 0;
    int16_t flow_y= 0;
    int64_t flow_x_sum= 0;
    int64_t flow_y_sum= 0;
    uint16_t lidar[4]= {0};//距离顺序 :前后左右
    float high_in= 0;
    uint32_t time_tick= 0;
    vector_3f acc_last = {0};
    vector_3f acc = {0}; //重力
    vector_3f gyro = {0};//角速度
    vector_3f mag = {0}; //磁力
    vector_3f euler = {0};//姿态角 欧拉姿态
    float quat[4] = {0};  //四元数

  }robo_info;

};

// 声明一个全局变量，用于发送数据
extern udp_mavlink * User_Send ;

#endif
